package com.grapes.view;

import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.grapes.R;
import com.grapes.model.Comment;
import com.grapes.model.Reply;
import com.grapes.model.Student;
import com.grapes.widget.GrapesHeadIconImgView;

public class TopicDetailActivity extends GrapesActivity{
    private static final String TOPIC_DETAIL="TopicDetail";
	private String topicDetail;
    private ListView replyListView;
    private Comment selectedTopic;
    private ReplyAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		topicDetail=getIntent().getStringExtra(TOPIC_DETAIL);
		replyListView=(ListView)findViewById(R.id.top_detail_reply_list);
		parseTopicDetail();
	}
	
    private void parseTopicDetail()
    {
    	JSONObject jsonObj=null;
    	try {
			jsonObj=new JSONObject(topicDetail);
			selectedTopic=new Comment();
			selectedTopic.setCommentId(jsonObj.getString("commentId"));
			selectedTopic.setContent(jsonObj.getString("comment_message"));
			selectedTopic.setPublishTimeStamp(jsonObj.getString("comment_timestamp"));
		    Student publisher=new Student();
		    JSONObject publisherOjb=jsonObj.getJSONObject("student");
		    publisher.setIconUrl(publisherOjb.getString("renrenIcon"));
		    publisher.setStudentName(publisherOjb.getString("renrenUserName"));
		    publisher.setSchoolName(publisherOjb.getString("schoolname"));
		    selectedTopic.setPublisher(publisher);
		    List<Reply> replies=new LinkedList<Reply>();
			JSONArray repliesOjb=jsonObj.getJSONArray("replies");
			for(int index=0;index<repliesOjb.length();index++)
			{
				Reply reply=new Reply();
				JSONObject replyObj=repliesOjb.getJSONObject(index);
				reply.setAttachedCommentId(selectedTopic.getCommentId());
				reply.setReplyId(replyObj.getString("replyId"));
				reply.setReplyTimeStamp(replyObj.getString("reply_timestamp"));
				reply.setReplyContent(replyObj.getString("reply_message"));
				JSONObject replierObj=replyObj.getJSONObject("student");
				Student replier=new Student();
				replier.setIconUrl(replierObj.getString("renrenIcon"));
				replier.setStudentName(replierObj.getString("renrenUserName"));
				replier.setSchoolName(replierObj.getString("schoolname"));
				reply.setReplier(replier);
				replies.add(reply);
			}
			selectedTopic.setAttchedReplies(replies);
		} catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "解析数据出错", 500).show();
			replyListView.setVisibility(View.GONE);
			return;
		}
    	   	
    	GrapesHeadIconImgView publisherIcon = (GrapesHeadIconImgView) 
				findViewById(R.id.company_commentor_icon);
    	TextView publishDateAndTime = (TextView)findViewById(R.id.company_commentor_publish_timestamp);
    	TextView topicContent = (TextView)findViewById(R.id.company_comment_content);
    	TextView replyCount = (TextView)findViewById(R.id.company_comment_reply_count);
    	TextView publisherMsg = (TextView)findViewById(R.id.company_commentor_name);
    	
    	publisherIcon.setResourceUrl(selectedTopic.getPublisher().getIconUrl());
    	publishDateAndTime.setText(selectedTopic.getPublishTimeStamp());
    	topicContent.setText(selectedTopic.getContent());
    	replyCount.setText(String.valueOf(selectedTopic.getAttchedReplies().size()));
    	publisherMsg.setText(selectedTopic.getPublisher().getStudentName());
    	
    	if(selectedTopic.getAttchedReplies().size()==0)
    	{
    		replyListView.setVisibility(View.GONE);
    	}else
    	{
    		adapter=new ReplyAdapter();
    		replyListView.setAdapter(adapter);
    	}
    }
    
	private class CellViewHolder {
		GrapesHeadIconImgView headIcon;
		TextView timestampTxt;
		TextView contentTxt;
		TextView nameTxt;
	}
    
    private class ReplyAdapter extends BaseAdapter
    {
		@Override
		public int getCount() {
			return selectedTopic.getAttchedReplies().size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CellViewHolder viewHolder;
			GrapesHeadIconImgView headIcon;
			TextView timestampTxt;
			TextView contentTxt;
			TextView nameTxt;

			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.reply_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.headIcon = (GrapesHeadIconImgView) convertView
						.findViewById(R.id.replier_icon);
				viewHolder.timestampTxt = (TextView) convertView
						.findViewById(R.id.replier_publish_timestamp);
				viewHolder.contentTxt = (TextView) convertView
						.findViewById(R.id.replier_content);
				viewHolder.nameTxt = (TextView) convertView
						.findViewById(R.id.replier_name);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			headIcon = viewHolder.headIcon;
			timestampTxt = viewHolder.timestampTxt;
			contentTxt = viewHolder.contentTxt;
			nameTxt = viewHolder.nameTxt;

            Reply reply=selectedTopic.getAttchedReplies().get(position);
            headIcon.setResourceUrl(reply.getReplier().getIconUrl());
            timestampTxt.setText(reply.getReplyTimeStamp());
            contentTxt.setText(reply.getReplyContent());
            nameTxt.setText(reply.getReplier().getStudentName());
			return convertView;
		}
    	
    }
}
